<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
namespace ProjetA\GroupeBundle\Form;

use Symfony\Component\Form\Form;
use Symfony\Component\HttpFoundation\Request;
use Doctrine\ORM\EntityManager;

use ProjetA\GroupeBundle\Entity\Groupe as Groupe;
use ProjetA\GroupeBundle\Entity\Page;
use ProjetA\GroupeBundle\Entity\Text;
use ProjetA\GroupeBundle\Entity\Tag;
use ProjetA\GroupeBundle\Entity\Date;
use FOS\UserBundle\Entity\UserManager as UserInterface;
/**
 * Description of GroupeHandler
 *
 * @author Gael
 */
class GroupeHandler {
    protected $form;
    protected $request;
    protected $em;
    protected $usermanager;
    
    public function __construct(Form $form, Request $request, EntityManager $em, UserInterface $userManager)
    {
        $this->form    = $form;
        $this->request = $request;
        $this->em      = $em;
        $this->usermanager  = $userManager;
    }

    public function process()
    {
        if( $this->request->getMethod() == 'POST' )
        {
            $this->form->bindRequest($this->request);

            if( $this->form->isValid() )
            {
                $data= $this->request;
                
                 return $this->onSuccess($data);
                 //return true;
            }
        }

        return false;
    }

 
    
    
    
    public function onSuccess($data)
    {
        
        $groupe = new Groupe();
        $groupe->setDatecreation(new \DateTime("now"));
        $groupe->setDescription($this->form->get('description')->getData());
        $groupe->setNom($this->form->get('nom')->getData());
        $groupe->setPublic($this->form->get('public')->getData());
        $page = $this->em->getRepository('ProjetAGroupeBundle:Page')->find($this->form->get('page_id')->getData());
        $moderateur = $this->usermanager->findUserBy(array('username'=>$this->form->get('moderateur_username')->getData()));
        $groupe->setPage($page);
        $groupe->setModerateur($moderateur);
        $moderateur->estModerateurDe($groupe);
        $groupe->setTags($this->form->get('tags')->getData());
        
        
        foreach ($groupe->getTags() as $tag) {
            $tag->setGroupe($groupe);
            $this->em->persist($tag);
            
        }
        //echo "<pre>" . print_r($this->form->get('page')->getData(), true) . "</pre>";
        //echo $page->findTypeAttribut("Nom");
        foreach($this->form->get('page')->getData()  as $key => $datapage)  
        {
            
            switch($page->findTypeAttribut($key)){
                case 'text':
                    $text = new Text();
                    $text->setText($datapage[$key]);
                    $text->setName($key);
                    $text->setGroupe($groupe);
                    $groupe->addText($text);
                    $this->em->persist($text);
                break;
                case 'email':
                    $text = new Text();
                    $text->setText($datapage[$key]);
                    $text->setName($key);
                    $text->setGroupe($groupe);
                    $groupe->addText($text);
                    $this->em->persist($text);
                break;
                case 'date':
                    $date = new Date();
                    $date->setDate($datapage[$key]);
                    $date->setName($key);
                    $date->setGroupe($groupe);
                    $groupe->addDate($date);
                    $this->em->persist($date);
                break;
            }
                  
            
        }
             
        $this->em->persist($groupe);
        $this->em->persist($moderateur); 
        $this->em->flush();
        return $groupe->getId();
        
    }
}

?>
